clear all
set more off

use "all_state_level_epu_wide.dta"

gen period = (year + (month/12))*12
tsset  period

local vars "EPU_BBD EPU_State EPU_National EPU_Composite"

foreach var of local vars {
	egen avg_`var' = rowmean(`var'*)
}

label var avg_EPU_National "EPU National"
label var avg_EPU_BBD "EPU BBD"
label var avg_EPU_State "EPU State"
label var ym "Date"
tsset period

STOP STOP STOP

******************************************************************************************************
***For Figure 2 (EPU-S and EPU-N Overall and Around Corona)
twoway line avg_EPU_National avg_EPU_State ym if year>=1985, xlabel(1985(5)2020) xtitle("") ylabel(50(100)300)
graph export "Results/EPU_Indexes_NoBBD_all.png", height(600) width(900) replace

twoway line avg_EPU_National avg_EPU_State ym if year>=2015, xlabel(2015(1)2020) xtitle("")
graph export "Results/EPU_Indexes_NoBBD_2015.png", height(600) width(900) replace


**********************************************************************************************************
**Correlations between state and national EPU
cap set obs 3000
set more off
gen corr_state_national_pre2020 = .
gen state = ""
local x = 1
foreach var of global states {
	cap corr EPU_National`var' EPU_State`var' if year<2020
	replace corr_state_national_pre2020 = r(rho) in `x'
	replace state = "`var'" in `x'
	local x = `x'+1
}

gen corr_state_national = .
local x = 1
foreach var of global states {
	cap corr EPU_National`var' EPU_State`var'
	replace corr_state_national = r(rho) in `x'
	replace state = "`var'" in `x'
	local x = `x'+1
}


sum corr_state_national corr_state_national_pre2020,d

**Pairwise correlations
cap set obs 3000
set more off
gen corrs_state = .
gen state1_state = ""
gen state2_state = ""
local x = 1
foreach var of global states {
	foreach var2 of global states {
		cap corr EPU_State`var' EPU_State`var2' if year<2020
		replace corrs_state = r(rho) in `x'
		replace state1_state = "`var'" in `x'
		replace state2_state = "`var2'" in `x'
		local x = `x'+1
	}
}

set more off
gen corrs_national = .
gen state1_national = ""
gen state2_national = ""
local x = 1
foreach var of global states {
	foreach var2 of global states {
		cap corr EPU_National`var' EPU_National`var2'   if year<2020
		replace corrs_national = r(rho) in `x'
		replace state1_national = "`var'" in `x'
		replace state2_national = "`var2'" in `x'
		local x = `x'+1
		
	}
}

set more off
gen corrs_bbd = .
gen state1_bbd = ""
gen state2_bbd = ""
local x = 1
foreach var of global states {
	foreach var2 of global states {
		cap corr EPU_BBD`var' EPU_BBD`var2'   if year<2020
		replace corrs_bbd = r(rho) in `x'
		replace state1_bbd = "`var'" in `x'
		replace state2_bbd = "`var2'" in `x'
		local x = `x'+1
		
	}
}

sum corrs_national corrs_state corrs_bbd,d 



********************************************************************************************
**Figure A2 - Pairwise correlations with national avg values - State Maps
global states "DC	 AL  AK  AZ  AR  CA  CO  CT  DE  FL  GA  HI  ID  IL  IN  IA  KS  KY  LA  ME  MD  MA  MI  MN  MS  MO  MT  NE  NV  NH  NJ  NM  NY  NC  ND  OH  OK  OR  PA  RI  SC  SD  TN  TX  UT  VT  VA  WA  WV  WI  WY"

set more off
gen corrs_national = .
gen corrs_state = .
gen corrs_composite = .
gen state = ""
local x = 1
foreach var of global states {
		replace state = "`var'" in `x'
		corr EPU_National`var' avg_EPU_National if year<2020 & year>=1990
		replace corrs_national = r(rho) if state=="`var'"
		corr EPU_State`var' avg_EPU_State if year<2020 & year>=1990
		replace corrs_state = r(rho) if state=="`var'"
		corr EPU_Composite`var' avg_EPU_Composite if year<2020 & year>=1990
		replace corrs_composite = r(rho) if state=="`var'"
		local x = `x'+1
		
}

keep state corrs*
drop if state==""

****State Figures
shp2dta using "tl_2010_us_state10.shp", database(usdb) coordinates(uscoord) genid(id) replace
gen STUSPS10=state 
merge m:1 STUSPS10 using usdb
drop if _merge==2
drop _merge

spmap corrs_composite using uscoord if STUSPS10!="AK"&STUSPS10!="HI" , id(id) fcolor(Reds2) clmethod(custom) clbreaks(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1) legend(size(medlarge))
graph export "epuc_national_correlations.png", height(600) width(1000) replace

spmap corrs_national using uscoord if STUSPS10!="AK"&STUSPS10!="HI" , id(id) fcolor(Reds2) clmethod(custom) clbreaks(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1) legend(size(medlarge))
graph export "epun_national_correlations.png", height(600) width(900) replace

spmap corrs_state using uscoord if STUSPS10!="AK"&STUSPS10!="HI" , id(id) fcolor(Reds2) clmethod(custom) clbreaks(0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 1) legend(size(medlarge))
graph export "epus_national_correlations.png", height(600) width(900) replace







************************************************************************************
**************BELOW CODE PRODUCES FIGURE A1*****************************************
************************************************************************************

********************************************************************************************
**Time series of correlations - State
preserve
cap set obs 3000
set more off

drop q_EPU* ratio*

sort year month
gen time_code = [_n]

forvalues time = 60/438 {
	gen corrs_state_`time' = .
}
gen state1_state = ""
gen state2_state = ""
replace ym = ym*12
tsset ym

foreach var of varlist EPU_State* {
	gen d_`var' = d.`var'
}

forvalues time = 60/438 {
	local x = 1
	foreach var of global states {
		foreach var2 of global states {
			cap corr d_EPU_State`var' d_EPU_State`var2' if time_code<=`time' & time_code>=`time'-59
			replace corrs_state_`time'= r(rho) in `x'
			replace state1_state = "`var'" in `x'
			replace state2_state = "`var2'" in `x'
			local x = `x'+1
		}
	}
}

forvalues time = 60/438 {
	egen avg_corrs_state_`time' = mean(corrs_state_`time')
	drop corrs_state_`time'
}

keep avg_corrs_state_*
keep in 1
gen temp = 1
reshape long avg_corrs_state_, i(temp) j(period)
drop temp
gen year = 1990 if period<72
gen month = mod(period, 12)+1
replace year = year[_n-12]+1 if year==.
gen ym = year + (month-1)/12

twoway line avg_corrs_state ym

compress
save "Results/avg_corrs_state.dta", replace
restore

********************************************************************************************
**Time series of correlations - National
preserve
cap set obs 3000
set more off

drop q_EPU* ratio*

sort year month
gen time_code = [_n]

forvalues time = 60/438 {
	gen corrs_national_`time' = .
}
gen state1_state = ""
gen state2_state = ""
replace ym = ym*12
tsset ym

foreach var of varlist EPU_National* {
	gen d_`var' = d.`var'
}

forvalues time = 60/438 {
	local x = 1
	foreach var of global states {
		foreach var2 of global states {
			cap corr d_EPU_National`var' d_EPU_National`var2' if time_code<=`time' & time_code>=`time'-59
			replace corrs_national_`time'= r(rho) in `x'
			replace state1_state = "`var'" in `x'
			replace state2_state = "`var2'" in `x'
			local x = `x'+1
		}
	}
}

forvalues time = 60/438 {
	egen avg_corrs_national_`time' = mean(corrs_national_`time')
	drop corrs_national_`time'
}

keep avg_corrs_national_*
keep in 1
gen temp = 1
reshape long avg_corrs_national_, i(temp) j(period)
drop temp
gen year = 1990 if period<72
gen month = mod(period, 12)+1
replace year = year[_n-12]+1 if year==.
gen ym = year + (month-1)/12

twoway line avg_corrs_national ym

compress
save "Results/avg_corrs_national.dta", replace

restore

use "Results/avg_corrs_national.dta", clear
merge 1:1 ym using "Results/avg_corrs_state.dta"
drop _merge

label var avg_corrs_nat "Average Pairwise Correlation of EPU-N Changes"
label var avg_corrs_state_ "Average Pairwise Correlation of EPU-S Changes"

twoway line avg* ym if year>=1995, legend(cols(1))
graph export "Results/average_state_pair_correlations.png", height(600) width(900) replace


********************************************************************************************
**Time series of correlations - State LEVELS
preserve
cap set obs 3000
set more off

drop q_EPU* ratio*

sort year month
gen time_code = [_n]

forvalues time = 60/438 {
	gen corrs_state_`time' = .
}
gen state1_state = ""
gen state2_state = ""
replace ym = ym*12
tsset ym

forvalues time = 60/438 {
	local x = 1
	foreach var of global states {
		foreach var2 of global states {
			cap corr EPU_State`var' EPU_State`var2' if time_code<=`time' & time_code>=`time'-59
			replace corrs_state_`time'= r(rho) in `x'
			replace state1_state = "`var'" in `x'
			replace state2_state = "`var2'" in `x'
			local x = `x'+1
		}
	}
}

forvalues time = 60/438 {
	egen avg_corrs_state_`time' = mean(corrs_state_`time')
	drop corrs_state_`time'
}

keep avg_corrs_state_*
keep in 1
gen temp = 1
reshape long avg_corrs_state_, i(temp) j(period)
drop temp
gen year = 1990 if period<72
gen month = mod(period, 12)+1
replace year = year[_n-12]+1 if year==.
gen ym = year + (month-1)/12

twoway line avg_corrs_state ym

compress
save "Results/avg_corrs_state_levels.dta", replace
restore

********************************************************************************************
**Time series of correlations - National LEVELS
preserve
cap set obs 3000
set more off

drop q_EPU* ratio*

sort year month
gen time_code = [_n]

forvalues time = 60/438 {
	gen corrs_national_`time' = .
}
gen state1_state = ""
gen state2_state = ""
replace ym = ym*12
tsset ym

forvalues time = 60/438 {
	local x = 1
	foreach var of global states {
		foreach var2 of global states {
			cap corr EPU_National`var' EPU_National`var2' if time_code<=`time' & time_code>=`time'-59
			replace corrs_national_`time'= r(rho) in `x'
			replace state1_state = "`var'" in `x'
			replace state2_state = "`var2'" in `x'
			local x = `x'+1
		}
	}
}

forvalues time = 60/438 {
	egen avg_corrs_national_`time' = mean(corrs_national_`time')
	drop corrs_national_`time'
}

keep avg_corrs_national_*
keep in 1
gen temp = 1
reshape long avg_corrs_national_, i(temp) j(period)
drop temp
gen year = 1990 if period<72
gen month = mod(period, 12)+1
replace year = year[_n-12]+1 if year==.
gen ym = year + (month-1)/12

twoway line avg_corrs_national ym

compress
save "Results/avg_corrs_national_levels.dta", replace

restore

use "Results/avg_corrs_national_levels.dta", clear
merge 1:1 ym using "Results/avg_corrs_state_levels.dta"
drop _merge


label var avg_corrs_nat "Average Pairwise Correlation of EPU-N Levels"
label var avg_corrs_state_ "Average Pairwise Correlation of EPU-S Levels"

twoway line avg* ym if year>=1995, legend(cols(1))
graph export "Results/average_state_pair_correlations_levels.png", height(600) width(900) replace




